import java.util.ArrayList;
import java.util.Collections;


public class Itemset {
    /* ======================================================
     * MEMBERS
     * ====================================================== */
    private ArrayList<String> _items;
    private ArrayList<Transaction> _transactions;
    private Double _support;

    /* ======================================================
     * CONSTRUCTOR
     * ====================================================== */
    public Itemset(String s){
	_items = new ArrayList<String>();
	_items.add(s);
	_transactions = new ArrayList<Transaction>();
	_support = 0.0;
    }

    public Itemset(ArrayList<String> s){
	_items = new ArrayList<String>();
	_items.addAll(s);
	_transactions = new ArrayList<Transaction>();
	_support = 0.0;
    }

    /* ======================================================
     * METHODS
     * ====================================================== */
    public void addItem(String s){
	_items.add(s);
	Collections.sort(_items);
    }

    public void addTransaction(Transaction t){
	if (!_transactions.contains(t)){
	    _transactions.add(t);    
	}
    }

    public boolean contains(ArrayList<String> list) {
	return _items.containsAll(list);
    }

    public String getItem(Integer index){
	return _items.get(index);
    }

    public ArrayList<String> getItems(){
	return _items;
    }

    public ArrayList<String> getKMinus1Subsets(){
	// This procedure generates all of the k - 1 subsets of an itemset
	ArrayList<String> returnKeyList = new ArrayList<String>();
	for (Integer i = 0; i < _items.size(); i++) {
	    ArrayList<String> key = new ArrayList<String>();
	    for (Integer j = 0; j < _items.size(); j++) {
		if (!i.equals(j)){
		    key.add(_items.get(j));
		}
	    }
	    returnKeyList.add(key.toString());
	}
	return returnKeyList;
    }

    public Double getSupport(){
	return _support;
    }
    
    public ArrayList<Transaction> getTransactions(){
	return _transactions;
    }

    public String hashKey(){
	return _items.toString();
    }

    public Integer size(){
	return _items.size();
    }

    public Integer numTransactions(){
	return _transactions.size();
    }

    public ArrayList<String> sublist(Integer from, Integer to){
	ArrayList<String> returnList = new ArrayList<String>();
	returnList.addAll(_items.subList(from, to));
	return returnList;
    }

    public void setSupport(Double d){
	_support = d;
    }

    public String toString(){
	return _items.toString();
    }
}
